package ninja.awesome.game.gameobject;

import java.util.Random;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.framework.Sound;
import ninja.awesome.game.Assets;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.classes.Hostile;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Physics;
import ninja.awesome.game.main.Sprites;

public class EnemyAttack extends GameObj implements Hostile {

	private Image[] image;
	private Image[] e;
	private int speed = 4;
	private boolean isDead;
	public boolean isBullet = true;
	private boolean hit = false;
	private String direction;
	private int timer = 0;
	private int swingTimer = 0;
	private int id;

	Random r = new Random();
	MainGameScreen game;
	Sprites s;
	Controller c;
	Animation anim;
	Animation explosion;
	Sound boom;

	double imageWidth = 150.0;
	double imageHeight = 151.0;

	public EnemyAttack(int x, int y, int speed, MainGameScreen game, Controller c, Sprites s, String direction, int robotID) {
		super(x, y);
		this.game = game;
		this.speed = speed;
		this.c = c;
		this.s = s;
		this.direction = direction;
		e = s.getExplosion();
		image = s.getEnemyAttack();
		this.id = robotID;
		if (robotID == 0) {
			anim = new Animation(0, image[0], imageWidth, imageHeight, 3);
			// explosion = new Animation(5, e[0], imageWidth, imageHeight, 1);
			boom = Assets.boom;
		} else if (robotID == 1) {
			anim = new Animation(0, image[1], 23, 82, 4);
			// explosion = new Animation(5, e[0], imageWidth, imageHeight, 1);
			boom = Assets.boom;
		}
	}

	@Override
	public void update() {
		if (id == 1) {
			if (direction.equals("left"))
				xLoc -= speed;
			else
				xLoc += speed;
			// PHU: update position related to speed of ninja
			xLoc -= game.getNinja().getSpeedX();
		}
		swingTimer++;
		if (id == 0 && swingTimer >= 30) {
			c.removeHostile(this);
		}
		if (!hit) {

			if (xLoc >= (MainGameScreen.WIDTH + 10) || xLoc <= -10 || yLoc < 10 || yLoc >= (MainGameScreen.HEIGHT  + 10)) {
				c.removeHostile(this);
			}
			if (direction.equals("left")) {
				anim.setAction(0);

			}
			if (direction.equals("right")) {
				anim.setAction(1);

			}

			if (Physics.collision(game.getNinja(), this)) {
				hit = true;
				boom.play(1f);
				game.getNinja().setHEALTH(game.getNinja().getHEALTH() - 30);
			}

			anim.runAnimation();
		} else if (hit) {
			if (timer > 40) {
				c.removeHostile(this);
				hit = false;
			}
			// explosion.runAnimation();
			timer++;
		}

	}

	@Override
	public void draw(Graphics g) {
		if (!hit) {

			anim.drawAnimation(g, xLoc, yLoc);
		} else if (hit) {
			anim.drawAnimation(g, xLoc, yLoc);
		}

	}

	@Override
	public Rectangle getBounds() {
		if (this.id == 0)
			return new Rectangle(xLoc, yLoc, 120, 151);
		else
			return new Rectangle(xLoc, yLoc, 10, 10);
	}

	@Override
	public int getX() {
		return this.xLoc;
	}

	@Override
	public int getY() {
		return this.yLoc;
	}

	@Override
	public void setIsDead(boolean n) {
		isDead = n;
	}

	@Override
	public boolean getIsDead() {
		return isDead;
	}

}
